//
//  CommentCell.m
//  CommentTest
//
//  Created by Chan on 15/5/19.
//  Copyright (c) 2015年 king. All rights reserved.
//

#import "CommentCell.h"
#import "UIImageView+WebCache.h"
#import "HAODEsingleton.h"


#import "UIButton+WebCache.h"
#import "HttpRequest.h"
#import "UIImageView+MJWebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#define  lineColor  [UIColor colorWithRed:228.00/255.0 green:228.00/255.0 blue:228.00/255.0 alpha:1]

#import "UIViewExt.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
@interface CommentCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *array;
    NSInteger collectionHeight;
    UIView *containerView;
    UICollectionView *collection;
    HAODEsingleton*commdity;
    int is;
    UIImageView*images;
}

@end
@implementation CommentCell
@synthesize mDelegate;
- (void)awakeFromNib {
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"collection";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    for (UIView *view in [cell.contentView subviews]) {
        [view removeFromSuperview];
    }
    
    images=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
    [images setImageWithURL:[NSURL URLWithString:_imageArray[indexPath.row]]  placeholderImage:[UIImage imageNamed:@"logo_normalss.png"]];
    images.tag = indexPath.row;
    
    images.userInteractionEnabled = YES;
    [images addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
    
    // 内容模式
    images.clipsToBounds = YES;
    images.contentMode = UIViewContentModeScaleAspectFill;
    [cell addSubview:images];
    
    return cell;
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return YES;
}
//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//
//
//
//}

- (void)tapImage:(UITapGestureRecognizer *)tap
{
    int count = _imageArray.count;
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 替换为中等尺寸图片
        NSString *url = [_imageArray[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        
        //        photo.srcImageView = image.subviews[i]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = tap.view.tag; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}
#define kYOfImageContainer   136.0

- (void)setComment:(CommentModel *)comment
{
    _comment = comment;
    _imageArray= comment.imagePicSmall;
   
    _redArray= comment.reqIdarr;
    _amtArray=comment.amtarr;
    
    commdity=[HAODEsingleton shared];
    [self .baojiaBtn.layer setCornerRadius:2.0]; //设置矩圆角半径
    [self .baojiaBtn.layer setBorderWidth:0];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){252/255.0, 54/255.0, 30/255.0, 1.0});
    [self .baojiaBtn.layer setBorderColor:tureBTncolorref];//边框颜色
    
    if ([comment.baojiaSat isEqualToString:@"N"]) {
       
        
        [self .baojiaBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
         self.baojiaBtn.backgroundColor=[UIColor colorWithRed:252/255.0 green:54/255.0 blue:30/255.0 alpha:1];
        [self.baojiaBtn setTitle:@"去报价" forState:UIControlStateNormal];
        [self.baojiaBtn addTarget:self action:@selector(baojiaBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.baojiaBtn.tag=comment.tag.intValue;
        [self .baojiaBtn setUserInteractionEnabled:YES];

    }
    if ([comment.baojiaSat isEqualToString:@"Y"]) {
        [self .baojiaBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:148/255.0 blue:152/255.0 alpha:1.0]
       forState:UIControlStateNormal];
        self.baojiaBtn.backgroundColor=[UIColor clearColor];
        [self.baojiaBtn setTitle:@"已报价" forState:UIControlStateNormal];
        [self .baojiaBtn setUserInteractionEnabled:NO];
    }
    
    if ([comment.baojiaSat isEqualToString:@"A"]) {
        [self .baojiaBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:148/255.0 blue:152/255.0 alpha:1.0]
                              forState:UIControlStateNormal];
        self.baojiaBtn.backgroundColor=[UIColor clearColor];
        [self.baojiaBtn setTitle:@"已采纳" forState:UIControlStateNormal];
        [self .baojiaBtn setUserInteractionEnabled:NO];
    }
    if ([comment.baojiaSat isEqualToString:@"C"]) {
        [self .baojiaBtn setTitleColor:zhonghuiColor
                              forState:UIControlStateNormal];
        self.baojiaBtn.backgroundColor=[UIColor clearColor];
        [self.baojiaBtn setTitle:@"已关闭" forState:UIControlStateNormal];
        [self .baojiaBtn setUserInteractionEnabled:NO];
    }
    if ([comment.baojiaSat isEqualToString:@"E"]) {
        [self .baojiaBtn setTitleColor:zhonghuiColor
                              forState:UIControlStateNormal];
        self.baojiaBtn.backgroundColor=[UIColor clearColor];
        [self.baojiaBtn setTitle:@"已完成" forState:UIControlStateNormal];
        [self .baojiaBtn setUserInteractionEnabled:NO];
    }

//   [self .baojiaBtn setUserInteractionEnabled:NO];
    
    _image.layer.masksToBounds = YES;
    _image.layer.cornerRadius = 35/2;
    _image.layer.borderColor = [UIColor whiteColor].CGColor;
    _image.layer.borderWidth = 0;
//  [_image setImageWithURL:[NSURL URLWithString:comment.headImageUrl]];
    [_image setImageWithURL:[NSURL URLWithString:comment.headImageUrl] placeholderImage:[UIImage imageNamed:@"b6e75e86e716002f262a6658f2b317a8.jpg"]];
    _image.contentMode = UIViewContentModeScaleAspectFit;
    _image.userInteractionEnabled = YES;
    
    self.diselabelview.layer.borderWidth = 0.35;
    self.diselabelview.layer.borderColor = zuiqianhuicolor.CGColor;
    self.nameLabel.text = comment.userName;
    self.dateLabel.text = comment.createdAt;
    
    
    
    NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGFloat textHeight;
    if ([comment.type isEqualToString:@""]) {
        textHeight = 35;
        self.textV.text =[NSString stringWithFormat:@"%@" ,@"暂无说明哦！"];
        
    }else{
        textHeight = [[NSString stringWithFormat:@"求购类别 %@ ¥ %.2f 左右", comment.type ,comment.amt.doubleValue]
                      boundingRectWithSize:CGSizeMake(ScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height +15;
        self.textV.text =[NSString stringWithFormat:@"求购类别 %@ ¥ %.2f 左右", comment.type ,comment.amt.doubleValue];
  

         NSInteger count1 = [[NSString stringWithFormat:@" ¥ %.f ",comment.amt.doubleValue] length];
         NSInteger count = [[NSString stringWithFormat:@"%@",comment.type] length];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:
                    [NSString stringWithFormat:@"求购类别 %@ ¥ %.2f 左右", comment.type ,comment.amt.doubleValue]];
        
        [AttributedStr addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:15]
         
                              range:NSMakeRange(count+5,count1+3)];
        
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:luseColor
         
                              range:NSMakeRange(count+5,count1+3)];
        
        
        self.textV.attributedText = AttributedStr;

    }
   

 
        [collection removeFromSuperview];
//        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//        layout.itemSize = CGSizeMake((ScreenWidth-45)/4 , (ScreenWidth-45)/4);
//        layout.minimumInteritemSpacing = 1;
//        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//        
//        
//        collectionHeight = (comment. imagePicSmall.count/4 +1) *(layout.itemSize.width + 10) -10;
//        
//        collection = [[UICollectionView alloc]initWithFrame:CGRectMake(12.5, textHeight+50+25, ScreenWidth-25, collectionHeight) collectionViewLayout:layout];
//        collection.backgroundColor = [UIColor clearColor];
//    
//        [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
//        collection.delegate = self;
//        collection.dataSource = self;
//        collection.userInteractionEnabled=YES;
//        [self.contentView addSubview:collection];
  
    
    
    
    
    if (comment.imagePicSmall>0) {
       
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置单元格的尺寸
    flowLayout.itemSize = CGSizeMake(collectionCellHeight, collectionCellHeight);
    
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,textHeight+60+15, ScreenWidth, collectionCellHeight+15*2) collectionViewLayout:flowLayout];
    collection.backgroundColor = [UIColor clearColor];
    collection.dataSource=self;
    collection.delegate=self;
    
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    
    
    int numberOfLines = (int)ceil([self collectionView:collection numberOfItemsInSection:0] / 4.0);
    
    CGRect frame = collection.frame;
    frame.size.height = numberOfLines *collectionCellHeight + (numberOfLines - 1) * 4 + 15*2;
    collection.frame = frame;
    
    [self.contentView addSubview:collection];
    
    
    }else{
    
    
//    
//        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//        //设置单元格的尺寸
//        flowLayout.itemSize = CGSizeMake(collectionCellHeight, collectionCellHeight);
//        
//        flowLayout.minimumInteritemSpacing = 5;
//        flowLayout.minimumLineSpacing = 5;
//        flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
//        
//        collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,textHeight+60+15, ScreenWidth, collectionCellHeight+15*2) collectionViewLayout:flowLayout];
//        collection.backgroundColor = [UIColor clearColor];
//        collection.dataSource=self;
//        collection.delegate=self;
//        
//        [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
//        
//        
//        int numberOfLines = (int)ceil([self collectionView:collection numberOfItemsInSection:0] / 4.0);
//        
//        CGRect frame = collection.frame;
//        frame.size.height = numberOfLines *collectionCellHeight + (numberOfLines - 1) * 4 + 15*2;
//        collection.frame = frame;
//        
//        [self.contentView addSubview:collection];

    
    
    }
    
    
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(15, 55, ScreenWidth-30, 0.35)];
    line.backgroundColor=LineColor;
    [self.contentView addSubview:line];
    
   
    
   
    
}

+ (CGFloat)cellHeight:(CommentModel *)commentModel
{
    // 手动计算一下containerView的高度，拿此高度再加其它控件的高度
    NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGFloat textHeight;
    if ([commentModel.type isEqualToString:@""]) {
        textHeight = 5;
    }else{
        textHeight = [[NSString stringWithFormat:@"求购类别 %@ ¥%.2f 左右", commentModel.type ,commentModel.amt.doubleValue]
                      boundingRectWithSize:CGSizeMake(ScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height +15;
    }
    
    
    CGFloat headImageHeight = 110;
    
    //collectionView height
    
    if (commentModel.imagePicSmall.count==0) {
        CGFloat collectionH =0;
        return  headImageHeight + textHeight + collectionH;
    }else{
    NSInteger arrayCount = commentModel.imagePicSmall.count;
        int i = 1;
            if (arrayCount % 5 == 0) {
                i++;
            }
            
    CGFloat collectionH = (i) *((ScreenWidth-50)/4 + 10) -10;
        return  headImageHeight + textHeight +15 + collectionH;
    }
    
}
-(void)baojiaBtn:(UIButton*)sender{
    NSLog(@"-------%ld",sender.tag);
    commdity.reqId= [NSString stringWithFormat:@"%@",_redArray[sender.tag]];
    commdity.xuqiuhow= [NSString stringWithFormat:@"%@",_amtArray[sender.tag]];
    
    [mDelegate tiaojiemian:sender];//跳回控制器


}



//- (void)tapImage:(UITapGestureRecognizer *)tap
//{
//    int tapIndex = tap.view.tag;
//    
//    int count = _imageArray.count;
//    //int viewTag = tap.view.tag;
//    // 1.封装图片数据
//    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
//    if (count > 3) {
//        for (int i = 0; i<count; i++) {
//            // 替换为中等尺寸图片
//            NSString *url = [_imageArray[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
//            MJPhoto *photo = [[MJPhoto alloc] init];
//            photo.url = [NSURL URLWithString:url]; // 图片路径
//            //photo.srcImageView = containerView.subviews[i]; // 来源于哪个UIImageView
//            photo.srcImageView = (UIImageView *)tap.view;
//            [photos addObject:photo];
//        }
//        
//    }else{
//        for (int i = 0; i<count; i++) {
//            // 替换为中等尺寸图片
//            NSString *url = [_imageArray[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
//            MJPhoto *photo = [[MJPhoto alloc] init];
//            photo.url = [NSURL URLWithString:url]; // 图片路径
//            //photo.srcImageView = containerView.subviews[i]; // 来源于哪个UIImageView
//            photo.srcImageView = (UIImageView *)tap.view;
//            [photos addObject:photo];
//        }
//    }
//    
//    // 2.显示相册
//    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
//    browser.currentPhotoIndex = tapIndex; // 弹出相册时显示的第一张图片是？
//    browser.photos = photos; // 设置所有的图片
//    [browser show];
//}
//
//- (void)allTapImage:(UITapGestureRecognizer *)tap
//{
//    //int tapIndex = tap.view.tag;
//    
//    int count = _imageArray.count;
//    //int viewTag = tap.view.tag;
//    // 1.封装图片数据
//    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
//    for (int i = 0; i<count; i++) {
//        // 替换为中等尺寸图片
//        NSString *url = [_imageArray[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
//        MJPhoto *photo = [[MJPhoto alloc] init];
//        photo.url = [NSURL URLWithString:url]; // 图片路径
//        //photo.srcImageView = containerView.subviews[i]; // 来源于哪个UIImageView
//        photo.srcImageView = (UIImageView *)tap.view;
//        [photos addObject:photo];
//    }
//    
//    // 2.显示相册
//    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
//    browser.currentPhotoIndex = 3; // 弹出相册时显示的第一张图片是？
//    browser.photos = photos; // 设置所有的图片
//    [browser show];
//}
//


@end
